package com.cloudcc.mobile.adapter;

import java.lang.reflect.Field;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.cloudframe.model.MenuModel;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;

public class MyMenuAdapter extends BaseExpandableListAdapter {

	private List<MenuModel> mData;

	private Context mContext;

	public MyMenuAdapter(List<MenuModel> data, Context mContext) {
		super();
		this.mData = data;
		this.mContext = mContext;
	}

	@Override
	public int getGroupCount() {
		return mData.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mData.get(groupPosition).tab.size();
	}

	@Override
	public MenuModel getGroup(int groupPosition) {
		return mData.get(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public MenuTabModel getChild(int groupPosition, int childPosition) {
		return mData.get(groupPosition).tab.get(childPosition);
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		MenuModel menu = mData.get(groupPosition);

		GroupViewHolder holder;
		View view;
		if (convertView == null) {
			view = LayoutInflater.from(mContext).inflate(
					R.layout.group_item_layout2, null);
			holder = new GroupViewHolder();
			holder.mGroupName = (TextView) view.findViewById(R.id.group_name);
			holder.imageview = (ImageView) view.findViewById(R.id.group_icon);
			view.setTag(holder);
		} else {
			view = convertView;
			holder = (GroupViewHolder) view.getTag();
		}
		holder.mGroupName.setText(menu.label);
		if (menu != null) {
			holder.imageview.setImageResource(R.drawable.drawable_expand_close);
		}
		// 点击展开
		if (isExpanded && menu != null) {
			holder.imageview.setImageResource(R.drawable.drawable_expand_open);
		}
		return view;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		MenuTabModel childMenu = mData.get(groupPosition).tab
				.get(childPosition);
		View view;
		ChildViewHolder holder;
		if (convertView == null) {
			view = LayoutInflater.from(mContext).inflate(
					R.layout.child_item_layout, null);
			holder = new ChildViewHolder();
			holder.mChildName = (TextView) view.findViewById(R.id.child_name);
			holder.mIcon = (ImageView) view.findViewById(R.id.child_icon);
			view.setTag(holder);
		} else {
			view = convertView;
			holder = (ChildViewHolder) view.getTag();
		}
		holder.mChildName.setText(childMenu.tab_name);

		try {
			if (StringUtils.isNoneBlank(childMenu.imageId)) {
				@SuppressWarnings("rawtypes")
				Class drawable = R.drawable.class;
				Field field = drawable.getField(childMenu.imageId);
				int id = field.getInt(field.getName());
				holder.mIcon.setImageBitmap(null);
				holder.mIcon.setBackgroundResource(id);
			}
		} catch (IllegalArgumentException e) {
		} catch (NoSuchFieldException e) {
		} catch (IllegalAccessException e) {
		}

		return view;
	}

	private static class GroupViewHolder {
		TextView mGroupName;
		ImageView imageview;
	}

	private static class ChildViewHolder {
		ImageView mIcon;
		TextView mChildName;
	}

}
